package fr.umlv.ig.adapter;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import fr.umlv.ig.model.Line;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Station;
import fr.umlv.ig.xml.MetroModelFactory;

public class Main {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws SAXException 
	 * @throws ParserConfigurationException 
	 */
	@SuppressWarnings("serial")
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		MetroModelFactory metroFactory = MetroModelFactory.newInstance();

		
		MetroModelImpl model = metroFactory.getMetroModelImpl();
		Line l = model.getLine("1");
		int count = l.getCountStation();
		for(int i =0; i< count ; i++ ) {
			System.out.println(model.getStation(l, i));
		}
		
		StationTreeModel tree = new StationTreeModel(model);
		
		JTree jtree = new JTree(tree);
		
		JPanel content = new JPanel();
		
	    content.add(new JScrollPane(jtree),BorderLayout.EAST);
	    
	    
	    final JFrame f = new JFrame("Station");
	    
        jtree.setCellRenderer(new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                if (value instanceof Line) {
                	Line l = (Line)value;
                    /* Victim #row would be wrong */
                    value="Ligne " + l.getNumber();
                }
                if (value instanceof Station) {
                	Station station = (Station)value;
                	
                    value= station.getName();
                }
                return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            }
        });
	    
	    
	        f.getContentPane().add(content);
	        f.setSize(650,300);
	        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        f.setVisible(true);
	        
	        
	}

}
